// Copyright Dotware Technologies 2005. Author Hadi Asghari.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace dwDbfViewer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.Panel panel1;
		private System.Data.DataView dataView1;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtPath;
		private System.Windows.Forms.Panel panel4;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnQuery;
		private System.Windows.Forms.TextBox txtFilter;
		private System.Windows.Forms.Label lblFilter;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.Panel panel5;
		private System.Windows.Forms.MenuItem menuExit;
		private System.Windows.Forms.MenuItem menuOpenDbf;
		private System.Windows.Forms.MenuItem menuSaveXml;
		private System.Windows.Forms.MenuItem menuAbout;
		private System.Windows.Forms.MenuItem menuOptions;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.DataGrid gridQuery;
		private System.Windows.Forms.DataGrid gridStructure;
		private System.Windows.Forms.DataGrid gridInfo;
		private System.Windows.Forms.StatusBarPanel statusPanelCount;
		private System.Windows.Forms.StatusBarPanel statusPanelInfo;
		private System.Windows.Forms.StatusBarPanel statusPanelProgress;
		private System.Windows.Forms.MenuItem menuHelp;
		private System.Windows.Forms.MenuItem menuGoto;
		private System.ComponentModel.IContainer components;

		public FormMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.statusPanelCount = new System.Windows.Forms.StatusBarPanel();
            this.statusPanelProgress = new System.Windows.Forms.StatusBarPanel();
            this.statusPanelInfo = new System.Windows.Forms.StatusBarPanel();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuOpenDbf = new System.Windows.Forms.MenuItem();
            this.menuSaveXml = new System.Windows.Forms.MenuItem();
            this.menuExit = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.menuGoto = new System.Windows.Forms.MenuItem();
            this.menuOptions = new System.Windows.Forms.MenuItem();
            this.menuItem6 = new System.Windows.Forms.MenuItem();
            this.menuAbout = new System.Windows.Forms.MenuItem();
            this.menuHelp = new System.Windows.Forms.MenuItem();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel4 = new System.Windows.Forms.Panel();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.panel5 = new System.Windows.Forms.Panel();
            this.gridQuery = new System.Windows.Forms.DataGrid();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnQuery = new System.Windows.Forms.Button();
            this.txtFilter = new System.Windows.Forms.TextBox();
            this.lblFilter = new System.Windows.Forms.Label();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.gridStructure = new System.Windows.Forms.DataGrid();
            this.tabPage3 = new System.Windows.Forms.TabPage();
            this.gridInfo = new System.Windows.Forms.DataGrid();
            this.panel3 = new System.Windows.Forms.Panel();
            this.btnOpen = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.txtPath = new System.Windows.Forms.TextBox();
            this.dataView1 = new System.Data.DataView();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.statusPanelCount)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusPanelProgress)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusPanelInfo)).BeginInit();
            this.panel1.SuspendLayout();
            this.panel4.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.panel5.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.gridQuery)).BeginInit();
            this.panel2.SuspendLayout();
            this.tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.gridStructure)).BeginInit();
            this.tabPage3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.gridInfo)).BeginInit();
            this.panel3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataView1)).BeginInit();
            this.SuspendLayout();
            // 
            // statusBar1
            // 
            this.statusBar1.Location = new System.Drawing.Point(0, 407);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
            this.statusPanelCount,
            this.statusPanelProgress,
            this.statusPanelInfo});
            this.statusBar1.ShowPanels = true;
            this.statusBar1.Size = new System.Drawing.Size(810, 26);
            this.statusBar1.TabIndex = 2;
            // 
            // statusPanelCount
            // 
            this.statusPanelCount.Name = "statusPanelCount";
            this.statusPanelCount.Width = 200;
            // 
            // statusPanelProgress
            // 
            this.statusPanelProgress.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.statusPanelProgress.Name = "statusPanelProgress";
            this.statusPanelProgress.Width = 579;
            // 
            // statusPanelInfo
            // 
            this.statusPanelInfo.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
            this.statusPanelInfo.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
            this.statusPanelInfo.Name = "statusPanelInfo";
            this.statusPanelInfo.Width = 10;
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1,
            this.menuItem5,
            this.menuItem6});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuOpenDbf,
            this.menuSaveXml,
            this.menuExit});
            this.menuItem1.Text = "&File";
            // 
            // menuOpenDbf
            // 
            this.menuOpenDbf.Index = 0;
            this.menuOpenDbf.Text = "&Open DBF";
            this.menuOpenDbf.Click += new System.EventHandler(this.menuOpenDbf_Click);
            // 
            // menuSaveXml
            // 
            this.menuSaveXml.Index = 1;
            this.menuSaveXml.Text = "&Save As XML";
            // 
            // menuExit
            // 
            this.menuExit.Index = 2;
            this.menuExit.Text = "E&xit";
            this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 1;
            this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuGoto,
            this.menuOptions});
            this.menuItem5.Text = "&Options";
            // 
            // menuGoto
            // 
            this.menuGoto.Index = 0;
            this.menuGoto.Shortcut = System.Windows.Forms.Shortcut.CtrlG;
            this.menuGoto.Text = "&Goto Record";
            this.menuGoto.Click += new System.EventHandler(this.menuGoto_Click);
            // 
            // menuOptions
            // 
            this.menuOptions.Index = 1;
            this.menuOptions.Text = "&Options";
            this.menuOptions.Click += new System.EventHandler(this.menuOptions_Click);
            // 
            // menuItem6
            // 
            this.menuItem6.Index = 2;
            this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuAbout,
            this.menuHelp});
            this.menuItem6.Text = "&Help";
            // 
            // menuAbout
            // 
            this.menuAbout.Index = 0;
            this.menuAbout.Text = "&About";
            // 
            // menuHelp
            // 
            this.menuHelp.Index = 1;
            this.menuHelp.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftH;
            this.menuHelp.Text = "dBase Filters";
            this.menuHelp.Click += new System.EventHandler(this.menuHelp_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.panel4);
            this.panel1.Controls.Add(this.panel3);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(810, 407);
            this.panel1.TabIndex = 3;
            // 
            // panel4
            // 
            this.panel4.Controls.Add(this.tabControl1);
            this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel4.Location = new System.Drawing.Point(0, 55);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(810, 352);
            this.panel4.TabIndex = 3;
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Controls.Add(this.tabPage3);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(810, 352);
            this.tabControl1.TabIndex = 2;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.panel5);
            this.tabPage1.Controls.Add(this.panel2);
            this.tabPage1.Location = new System.Drawing.Point(4, 25);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(802, 323);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Data Grid";
            // 
            // panel5
            // 
            this.panel5.Controls.Add(this.gridQuery);
            this.panel5.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel5.Location = new System.Drawing.Point(0, 37);
            this.panel5.Name = "panel5";
            this.panel5.Size = new System.Drawing.Size(802, 286);
            this.panel5.TabIndex = 2;
            // 
            // gridQuery
            // 
            this.gridQuery.DataMember = "";
            this.gridQuery.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gridQuery.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.gridQuery.Location = new System.Drawing.Point(0, 0);
            this.gridQuery.Name = "gridQuery";
            this.gridQuery.Size = new System.Drawing.Size(802, 286);
            this.gridQuery.TabIndex = 0;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnQuery);
            this.panel2.Controls.Add(this.txtFilter);
            this.panel2.Controls.Add(this.lblFilter);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(802, 37);
            this.panel2.TabIndex = 1;
            // 
            // btnQuery
            // 
            this.btnQuery.Location = new System.Drawing.Point(576, 6);
            this.btnQuery.Name = "btnQuery";
            this.btnQuery.Size = new System.Drawing.Size(90, 26);
            this.btnQuery.TabIndex = 2;
            this.btnQuery.Text = "&Query";
            this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
            // 
            // txtFilter
            // 
            this.txtFilter.Location = new System.Drawing.Point(96, 7);
            this.txtFilter.Name = "txtFilter";
            this.txtFilter.Size = new System.Drawing.Size(451, 22);
            this.txtFilter.TabIndex = 1;
            this.txtFilter.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtFilter_KeyPress);
            // 
            // lblFilter
            // 
            this.lblFilter.Location = new System.Drawing.Point(10, 9);
            this.lblFilter.Name = "lblFilter";
            this.lblFilter.Size = new System.Drawing.Size(96, 19);
            this.lblFilter.TabIndex = 0;
            this.lblFilter.Text = "dBase &Filter:";
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.gridStructure);
            this.tabPage2.Location = new System.Drawing.Point(4, 25);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(672, 323);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Table Structure";
            this.tabPage2.Visible = false;
            // 
            // gridStructure
            // 
            this.gridStructure.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.gridStructure.DataMember = "";
            this.gridStructure.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gridStructure.FlatMode = true;
            this.gridStructure.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.gridStructure.Location = new System.Drawing.Point(0, 0);
            this.gridStructure.Name = "gridStructure";
            this.gridStructure.ReadOnly = true;
            this.gridStructure.Size = new System.Drawing.Size(672, 323);
            this.gridStructure.TabIndex = 0;
            // 
            // tabPage3
            // 
            this.tabPage3.Controls.Add(this.gridInfo);
            this.tabPage3.Location = new System.Drawing.Point(4, 25);
            this.tabPage3.Name = "tabPage3";
            this.tabPage3.Size = new System.Drawing.Size(672, 323);
            this.tabPage3.TabIndex = 2;
            this.tabPage3.Text = "Dbf Info";
            this.tabPage3.Visible = false;
            // 
            // gridInfo
            // 
            this.gridInfo.DataMember = "";
            this.gridInfo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gridInfo.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.gridInfo.Location = new System.Drawing.Point(0, 0);
            this.gridInfo.Name = "gridInfo";
            this.gridInfo.ReadOnly = true;
            this.gridInfo.Size = new System.Drawing.Size(672, 323);
            this.gridInfo.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel3.Controls.Add(this.btnOpen);
            this.panel3.Controls.Add(this.label1);
            this.panel3.Controls.Add(this.txtPath);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel3.Location = new System.Drawing.Point(0, 0);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(810, 55);
            this.panel3.TabIndex = 2;
            // 
            // btnOpen
            // 
            this.btnOpen.Image = ((System.Drawing.Image)(resources.GetObject("btnOpen.Image")));
            this.btnOpen.Location = new System.Drawing.Point(720, 12);
            this.btnOpen.Name = "btnOpen";
            this.btnOpen.Size = new System.Drawing.Size(58, 26);
            this.btnOpen.TabIndex = 7;
            this.btnOpen.Click += new System.EventHandler(this.menuOpenDbf_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(10, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(86, 19);
            this.label1.TabIndex = 6;
            this.label1.Text = "dBase Path:";
            // 
            // txtPath
            // 
            this.txtPath.Location = new System.Drawing.Point(96, 15);
            this.txtPath.Name = "txtPath";
            this.txtPath.Size = new System.Drawing.Size(605, 22);
            this.txtPath.TabIndex = 5;
            this.txtPath.Text = "..\\..\\..\\example-farsi-dbf\\titel.dbf";
            this.txtPath.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPath_KeyPress);
            // 
            // imageList1
            // 
            this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // FormMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
            this.ClientSize = new System.Drawing.Size(810, 433);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.statusBar1);
            this.Menu = this.mainMenu1;
            this.Name = "FormMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Dotware Dbf Viewer";
            ((System.ComponentModel.ISupportInitialize)(this.statusPanelCount)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusPanelProgress)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusPanelInfo)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel4.ResumeLayout(false);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.panel5.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.gridQuery)).EndInit();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.gridStructure)).EndInit();
            this.tabPage3.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.gridInfo)).EndInit();
            this.panel3.ResumeLayout(false);
            this.panel3.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataView1)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FormMain());
		}

		dwDbfComLib.DBaseRecordSetClass dbfrs;

		private void btnQuery_Click(object sender, System.EventArgs e)
		{
			int maxData = Singleton.Instance().MaxRecords;

			DateTime startTime = DateTime.Now;
			
			
			if ( dbfrs == null ) // dbfrs.IsOpen() != 0 )
			{
				dbfrs = new dwDbfComLib.DBaseRecordSetClass();
				dbfrs.Open( txtPath.Text );
			}

			byte findRes = 1;
			if (txtFilter.Text == "" )
				
                
                dbfrs.MoveFirst();
			else 
				findRes = dbfrs.FindFirst( txtFilter.Text );
			
			uint recordCount = dbfrs.GetRecordCount();
			short fieldCount  = dbfrs.GetFieldCount();

			DataTable table = new DataTable();
			
			// create table in dsGrid with our format
			for (short i = 0; i < fieldCount; i++)
			{
				string name;
				sbyte ctype;
				short length;
				short dec;

				dbfrs.GetFieldInfo(i, out name, out ctype, out length, out dec);
			
				table.Columns.Add(name, GetDbfType(ctype) );
			}
			

			if ( Singleton.Instance().StartRecord != 0 )
				dbfrs.Position = Singleton.Instance().StartRecord;

			int index = 0;


			while (dbfrs.IsEof() != 1 && findRes != 0 && index < maxData)
			{
				IntPtr ptr = dbfrs.GetRecord();
				object [] record = 
					Marshal.GetObjectsForNativeVariants( ptr, fieldCount );
				dbfrs.FreeRecord( ptr.ToInt32() );

				table.Rows.Add( record );
				
				if ( index++ % 100 == 0 )
				{
					statusPanelCount.Text = string.Format( "{0} records loaded.....", index );
				}
				
				if (txtFilter.Text == "" )
					dbfrs.MoveNext();
				else 
					findRes = dbfrs.FindNext( txtFilter.Text );
			}

			gridQuery.DataSource = table; 
			statusPanelCount.Text = string.Format( "{0} records loaded.", index );

			ShowTime( DateTime.Now - startTime );
			MessageBox.Show(dbfrs.Position.ToString());

			dbfrs.Close();
			dbfrs = null;


			//
		}

		private void ShowTime( TimeSpan time )
		{
			if ( time.Seconds == 0 )
				statusPanelInfo.Text = string.Format("Query executed in {0} ms.", time.Milliseconds);
			else if ( time.Seconds < 60 )
				statusPanelInfo.Text = string.Format("Query executed in {0} s.", time.Seconds);
			else
				statusPanelInfo.Text = string.Format("Query executed in {0} m {1} s.", 
					time.Minutes, time.Seconds % 60 );
		}

		private void menuExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void menuOpenDbf_Click(object sender, System.EventArgs e)
		{
			openFileDialog.Filter = "dbf files (*.dbf)|*.dbf|All files (*.*)|*.*";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				txtPath.Text = openFileDialog.FileName;
				LoadDbfInfo();
			}	
		}

		private void menuOptions_Click(object sender, System.EventArgs e)
		{
			Form f = new FormOptions();
			f.ShowDialog();
		}

		private void txtPath_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if ( e.KeyChar == '\r' )
			{
				LoadDbfInfo();
			}
		}

		private void LoadDbfInfo()
		{
			dbfrs = new dwDbfComLib.DBaseRecordSetClass(); // close if not null?
			dbfrs.Open( txtPath.Text );

			DataTable structure = new DataTable();
			structure.Columns.Add( "Field Name", typeof (string) );
			structure.Columns.Add( "Type", typeof (string) );
			structure.Columns.Add( "Length", typeof (string) );

			DataTable information = new DataTable();
			information.Columns.Add( "Name", typeof (string) );
			information.Columns.Add( "Value", typeof (string) );


			// fill info
			uint recordCount = dbfrs.GetRecordCount();
			short fieldCount  = dbfrs.GetFieldCount();
			
			information.Rows.Add( new object [] { "Record", recordCount } );
			information.Rows.Add( new object [] { "Fields", fieldCount } );
			information.Rows.Add( new object [] { "Dbf-Id", dbfrs.DbfId } );
			information.Rows.Add( new object [] { "Dbf-Code-page", dbfrs.DbfCodePage } );
			information.Rows.Add( new object [] { "Table flags", dbfrs.TableFlag } );
			//information.Rows.Add( new object [] { "Last update", dbfrs.LastUpdate } );
			information.Rows.Add( new object [] { "Record-size", dbfrs.DbfRecordSize } );

			// fill structure	
			for (short i = 0; i < fieldCount; i++)
			{
				string name;
				sbyte ctype;
				short length;
				short dec;

				dbfrs.GetFieldInfo(i, out name, out ctype, out length, out dec);
					
				object [] ao = new object [3];
				ao[0] = name;
				ao[1] = string.Format(
					"{0} - {1}", 
					(char) ctype,
					GetDbfType(ctype).ToString()
					);
				if (dec != 0 )
					ao[2] = string.Format( "{0}({1})", length, dec );
				else 
					ao[2] = length.ToString();
				structure.Rows.Add( ao );
			}


			// TODO: set grid size and style
			gridInfo.DataSource = information;
			gridStructure.DataSource = structure;

			statusPanelInfo.Text = "Dbf information loaded.";

			dbfrs.Close();
			dbfrs = null;
		}


		private Type GetDbfType(sbyte ctype)
		{
			Type type;
			switch ( (char) ctype )
			{
				case 'F':
					goto case 'N';  // !?!
				case 'C':
					type = typeof ( string ) ;
					break;
				case 'N':
					type = typeof ( decimal );
					break;
				case 'L':
					type = typeof ( bool );
					break;
				case 'D':
					type = typeof ( DateTime );
					break;

				default:
					//MessageBox.Show( "Unknown dbf field type : " + (char) ctype );
					goto case 'M';
				case 'M':
					type = typeof ( object ); 
					break;
			}
			
			return type;
		}

		private void txtFilter_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (e.KeyChar == 'r')
				btnQuery_Click(sender, e);
		}

		private void menuHelp_Click(object sender, System.EventArgs e)
		{
			Form f = new FormHelp();
			f.Show();
		}


		private void menuGoto_Click(object sender, System.EventArgs e)
		{
			Form f = new FormGoto();
			f.ShowDialog();
			btnQuery_Click(sender, e);	
		}
	}
}